package cn.echo42.config;

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author DaiZhiZhou
 * @file Spring-Data-JPA
 * @create 2020-07-31 22:49
 */

@Configuration
@EnableJpaRepositories(
        basePackages = "cn.echo42.repository",
        transactionManagerRef = "jpaTransactionManager",
        entityManagerFactoryRef = "entityManagerFactory"
)
public class SpringDataJpaConfiguration {

    @Bean("jpaProperties")
    public Properties getProperties(){
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto","update");
        return properties;
    }

    @Bean("persistenceProvider") /* Jpa实现者 即规范供应商 */
    public HibernatePersistenceProvider getProvider(){
        return new HibernatePersistenceProvider();
    }

    @Bean("jpaDialect") /* Jpa方言对象 */
    public HibernateJpaDialect getHibernateJpaDialect(){
        return new HibernateJpaDialect();
    }

    @Bean("adapter") /* 实现者适配器 */
    public HibernateJpaVendorAdapter getHibernateJpaVendorAdapter(){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.MYSQL); /* 数据库类型 */
        adapter.setGenerateDdl(false); /* 自动创建数据库表 */
        adapter.setShowSql(true); /* 打印执行的SQL语句 */
        adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect"); /* 数据库平台设置:MYSQL方言 */
        return adapter;
    }

    @Bean("entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean getEntityManagerFactoryBean (
            DataSource dataSource,
            @Value("cn.echo42.domain") String scanPath, /* 实体类的所在包扫描 */
            HibernatePersistenceProvider hibernatePersistenceProvider, /* 注入持久化供应商 */
            HibernateJpaDialect hibernateJpaDialect, /* JPQL 方言 注入 */
            @Qualifier("jpaProperties") Properties properties) {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(dataSource); /* 数据源注入 */
        factoryBean.setPackagesToScan(scanPath); /* 扫描路径注入 */
        factoryBean.setPersistenceProvider(hibernatePersistenceProvider); /* 供应商注入 */
        factoryBean.setJpaDialect(hibernateJpaDialect); /* 方言注入 */
        factoryBean.setJpaProperties(properties); /* Jpa配置信息 */
        return factoryBean;
    }

    @Bean("jpaTransactionManager") /* Jpa事务管理器 */
    public JpaTransactionManager getJpaTransactionManager(EntityManagerFactory factoryBean) {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(factoryBean);
        return jpaTransactionManager;
    }

}
